package com.company.pedidos.web.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.company.canonical.beans.JsonJTableVendedorBean;
import com.company.pedidos.service.PedidosService;
import com.company.pedidos.web.command.VendedorCommand;
import com.company.pedidos.web.datatable.response.JtableJsonListResponse;

@RequestMapping("/vendedor")
@Controller
public class VendedorController {

	private static final Logger logger = Logger
			.getLogger(VendedorController.class);

	@Autowired
	private PedidosService pedidosService;

	@Value("#{configuration['vendedor.controller.target.path.file']}")
	private String targetPath;

	public VendedorController() {

	}

	@ModelAttribute("vendedor")
	public VendedorCommand crearVendedorModel() {
		logger.debug("creating @ModelAttribute object");
		VendedorCommand command = new VendedorCommand();
		return command;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/mostrar", method = RequestMethod.GET)
	public String inicioConsultaVendedor(ModelMap modelMap) {
		return "vendedor/listaVendedor";
	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/buscar", method = RequestMethod.POST)
	@ResponseBody
	public JtableJsonListResponse<JsonJTableVendedorBean> buscarVendedor(
			Pageable pageable,
			@ModelAttribute(value = "vendedor") VendedorCommand vendedor,
			@RequestParam(required = false, value = "jtStartIndex") int jtStartIndex,
			@RequestParam(required = false, value = "jtPageSize") int jtPageSize) {
		JtableJsonListResponse<JsonJTableVendedorBean> jsonString;
		List<JsonJTableVendedorBean> listaVendedor;
		try {
			 Long expenseCount = pedidosService.buscarVendedorCount(
						vendedor.getNombres(), vendedor.getApellidoPaterno(),
						vendedor.getApellidoMaterno());
			listaVendedor = pedidosService.buscarVendedor(
					vendedor.getNombres(), vendedor.getApellidoPaterno(),
					vendedor.getApellidoMaterno(), jtStartIndex, jtPageSize);
			jsonString = new JtableJsonListResponse<JsonJTableVendedorBean>(
					"OK", listaVendedor, expenseCount.intValue());
		} catch (Exception e) {
			jsonString = new JtableJsonListResponse<JsonJTableVendedorBean>(
					"ERROR", e.getMessage());
		}
		return jsonString;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/vendedor/upload", method = RequestMethod.GET)
	public String uploadInit() {
		return "uploadVendedor";
	}

	/**
	 * 
	 * @param name
	 * @param file
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/vendedor/upload", method = RequestMethod.POST)
	public String handleFileUpload(@RequestParam("file") MultipartFile file)
			throws Exception {

		if (!file.isEmpty()) {

			logger.debug(" the file " + file.getOriginalFilename()
					+ "was uploaded");
			byte[] bytes = file.getBytes();
			BufferedOutputStream stream = new BufferedOutputStream(
					new FileOutputStream(new File(targetPath
							+ file.getOriginalFilename())));
			stream.write(bytes);
			stream.close();
			return "uploadVendedor";

		}

		return "";
	}

}
